package com.phospherus.user.controller

import com.phospherus.common.controller.BaseController
import com.phospherus.common.controller.handler.ResultHandler
import com.phospherus.user.entity.User
import com.phospherus.user.service.LoginResultHandler
import com.phospherus.user.service.UserService
import org.springframework.stereotype.Controller
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RequestMethod
import org.springframework.web.bind.annotation.RequestParam
import org.springframework.web.bind.annotation.ResponseBody
import javax.servlet.http.HttpServletRequest

/**
 * @author songjinlong
 * @version 1.0
 * @since 2017/11/8
 */
@Controller
@RequestMapping("/user")
class UserController : BaseController<User, UserService>() {


    /**
     * 用户登录
     */
    @RequestMapping("/login")
    @ResponseBody
    fun login(account: String, passWord: String, request: HttpServletRequest): ResultHandler<LoginResultHandler> {
        var ur = service.login(account, passWord) ?: return ResultHandler(0, "登录失败")
        request.session.setAttribute("USER", ur.user)
        request.session.setAttribute("RESOURCES", ur.resources)
        return ResultHandler(ResultHandler.SystemMessage.SUCCESS, ur)
    }

    /**
     * 用户信息
     */
    @RequestMapping("/info")
    @ResponseBody
    fun getUserInfo(request: HttpServletRequest): ResultHandler<User?> {
        var user: User? = request.session.getAttribute("USER") as User? ?: return ResultHandler(ResultHandler.SystemMessage.SYSTEM_ERROR)
        return ResultHandler(ResultHandler.SystemMessage.SUCCESS, user)
    }

    /**
     * 用户资源
     */
    @RequestMapping("/resources")
    @ResponseBody
    fun getUserResources(request: HttpServletRequest): ResultHandler<Any> {
        return ResultHandler(ResultHandler.SystemMessage.SUCCESS, request.session.getAttribute("RESOURCES"))
    }

    /**
     * 用户登出
     */
    @RequestMapping("/logout")
    @ResponseBody
    fun logout(request: HttpServletRequest): ResultHandler<Any> {
        request.session.removeAttribute("USER")
        request.session.removeAttribute("RESOURCES")
        return ResultHandler(ResultHandler.SystemMessage.SUCCESS)
    }

    /**
     * 新建用户
     */
    @RequestMapping("/createUser", method = [RequestMethod.GET, RequestMethod.POST])
    @ResponseBody
    fun createUser(user: User, isLeader: Boolean, roleId: String): ResultHandler<User?> {
        user.isLeader = isLeader
        var roleIds = roleId.split(",")
        var (user, passWord) = service.createUser(user, roleIds)
        user.passWord = passWord
        return ResultHandler(ResultHandler.SystemMessage.SUCCESS, user)
    }

    @RequestMapping("/modifyUserInfo", method = [RequestMethod.GET, RequestMethod.POST])
    @ResponseBody
    fun modifyUserInfo(@RequestParam map: Map<String, Any>, request: HttpServletRequest): ResultHandler<Any?> {
        var user = request.session.getAttribute("USER") as User
        var userId: Long = user?.id ?: return ResultHandler(0, "请重新登录系统后，修改信息")
        var email: String = (map["email"] as String?) ?: ""
        var phone: String = (map["phone"] as String?) ?: ""
        if (!service.modifyUserInfo(userId, email, phone)) return ResultHandler(ResultHandler.SystemMessage.SYSTEM_ERROR)
        request.session.setAttribute("USER", with(user) {
            this.phone = phone
            this.email = email
            this
        })
        return ResultHandler(ResultHandler.SystemMessage.SUCCESS)
    }

    @RequestMapping("/resetPassWord", method = [RequestMethod.GET, RequestMethod.POST])
    @ResponseBody
    fun resetPassWord(userId: Long): ResultHandler<String> {
        return ResultHandler(ResultHandler.SystemMessage.SUCCESS, service.resetPassWord(userId))
    }

    @RequestMapping("/modifyPassWord", method = [RequestMethod.GET, RequestMethod.POST])
    @ResponseBody
    fun modifyPassWord(@RequestParam map: Map<String, Any>, request: HttpServletRequest): ResultHandler<Any?> {
        var oldPassWord: String = (map["oldPassWord"] as String?) ?: return ResultHandler(0, "oldPassWord is null")
        var newPassWord: String = (map["newPassWord"] as String?) ?: ""
        var user = request.session.getAttribute("USER") as User
        if (!service.modifyPassWord(oldPassWord, newPassWord, user)) return ResultHandler(ResultHandler.SystemMessage.SYSTEM_ERROR)
        return ResultHandler(ResultHandler.SystemMessage.SUCCESS)
    }

    @RequestMapping("/modifyUser", method = [RequestMethod.GET, RequestMethod.POST])
    @ResponseBody
    fun modifyUser(userId: Long, orgId: Long, isLeader: Boolean, roleId: String): ResultHandler<Any?> {
        var roleIds = roleId.split(",")
        service.modifyUser(userId, orgId, isLeader, roleIds)
        return ResultHandler(ResultHandler.SystemMessage.SUCCESS)
    }

    @RequestMapping("/delete", method = [RequestMethod.GET, RequestMethod.POST])
    @ResponseBody
    fun modifyUser(userId: Long): ResultHandler<Any?> {
        service.delete(userId)
        return ResultHandler(ResultHandler.SystemMessage.SUCCESS)
    }
}